<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="对账批次" prop="checkBatchNo">
        <el-input
          v-model="queryParams.checkBatchNo"
          placeholder="请输入对账批次"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="clearingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="对账批次" align="center" prop="checkBatchNo" />
      <el-table-column label="商户名称" align="center" prop="businessName" />
      <el-table-column label="结算金额" align="center" prop="amount" />
      <el-table-column label="交易日期" align="center" prop="tradeDateZf" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tradeDateZf, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="toNotarizeClearing">确认结算</el-button>
  </div>
</template>

<script>
import { listClearing, getClearing, delClearing, addClearing, updateClearing,notarizeClearing } from "@/api/financing/clearing";

export default {
  name: "Clearing",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算表格数据
      clearingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        checkBatchNo: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询结算列表 */
    getList() {
      this.loading = true;
      listClearing(this.queryParams).then(response => {
        this.clearingList = response.data;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams={};
      this.handleQuery();
    },
    /*确认结算*/
    toNotarizeClearing() {
      if (this.ids.length < 1) {
        this.$message.error("请选择需要结算的数据");
        return;
      }
      notarizeClearing(this.ids).then(response=>{
        if (response.data) {
          // console.log(this.ids);
          this.$message.success("结算成功");
          this.getList();
        }else {
          this.$message.error("结算失败");
        }
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClearing(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改结算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClearing(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClearing(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除结算编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClearing(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    }
  }
};
</script>
